package com.ruix.app.ui.user.address;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.SuperAdapter;
import com.ruix.app.adapter.superadapter.internal.SuperViewHolder;
import com.ruix.app.ui.base.BaseActivity;

import java.util.Arrays;
import java.util.List;

/**
 * Created by hadexs on 2017/4/18.
 */

public class CityChoseActivity extends BaseActivity implements AdapterView.OnItemClickListener{


    public static Intent newIntent(Context context, String region){
        Intent intent = new Intent(context,CityChoseActivity.class);
        intent.putExtra("region",region);
        return intent;
    }

    private List<String> regions;
    private ListView listView;
    private ProvinceListAdapter adapter;


    @Override
    protected int loadViewLayout() {
        return R.layout.activity_country_chose;
    }

    @Override
    protected void initView() {
        listView =  (ListView)findViewById(R.id.lv_address);
        adapter = new ProvinceListAdapter(this);
        listView.setAdapter(adapter);
        setTitle("地区");
    }

    @Override
    protected void initLocalData() {
       Intent intent =  getIntent();
        if (intent != null){
            String region =   intent.getStringExtra("region");
            if (region != null){
                String[] regionses =  region.split("_");
                if (regionses != null) {
                    regions = Arrays.asList(regionses);
                }
            }
        }

        if (regions != null){
            adapter.replaceAll(regions);
        }

    }

    @Override
    protected void initListener() {
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String resion =  regions.get(position);
        goBack(resion);

    }

    private void goBack(String resion){
        Intent intent = new Intent();
        intent.putExtra("resion",resion);
        setResult(10012,intent);
        finish();
    }

    class  ProvinceListAdapter extends SuperAdapter<String> {

        public ProvinceListAdapter(Context context) {
            super(context, R.layout.item_address_choose);
        }

        @Override
        public void onBind(SuperViewHolder holder, int viewType, int position, String item) {
            holder.setText(R.id.tv_address_name,item);
        }
    }
}
